package items
{
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.*;
	
	import utilities.*;
	
	/**
	 * ...
	 * @author JAC
	 */
	public class SpeedBooster extends Item
	{
		private var _emitter:Emitter;
		private var _soundTimer:FlxDelay;
		
		public function SpeedBooster(i:uint = 0, j:uint = 0)
		{
			super(i, j);
			
			_duration = 5;
			
			loadGraphic(Assets.ImgItemSpeedBoost);
			
			toggleLight();
			
			_emitter = new Emitter(0, 0, 100);
			_emitter.createParticles(5, 5, 0xAA0042FF);
			_emitter.start(false, 0.25, 0.01);
			
			_soundTimer = new FlxDelay(1000);
		}
		
		override public function onUse(p:Player):Boolean
		{
			super.onUse(p);
			
			if (StatusEffect.speedBoosted)
			{
				_used = false;
				return false;
			}
			
			_emitter.at(p);
			Registry.misc.add(_emitter);
			
			FlxG.play(Assets.SndSpeed, Registry.soundVolume);
			_soundTimer.start();
			
			StatusEffect.speedBoosted = true;
			
			p.offsetSpeed(+50);
			
			_timer.reset(_duration * 1000);
			
			return true;
		}
		
		override public function update():void
		{
			super.update();
			
			if (_used)
			{
				_emitter.at(Player.player);
				
				if (_soundTimer.hasExpired)
				{
					_soundTimer.start();
					FlxG.play(Assets.SndSpeed, Registry.soundVolume);
				}
				
				if (_timer.hasExpired || !Player.isAlive)
				{
					Registry.misc.remove(_emitter);
					Player.player.offsetSpeed(-50);
					StatusEffect.speedBoosted = false;
					destroy();
					return;
				}
			}
		}
	}
}